import { get, post, put, del } from '@/utils/request'
import type { Soul } from './soul.service'

export interface Reincarnation {
  id: string
  time: string
  officer: string
  status: 'pending' | 'processing' | 'completed'
  location: string
  newIdentity: string
  reason: string
  conditions: string
  remarks: string
  soul: Soul
}

export interface ReincarnationParams {
  keyword?: string
  status?: string
  startTime?: string
  endTime?: string
  page: number
  pageSize: number
}

export interface ReincarnationStats {
  total: number
  pending: number
  processing: number
  completed: number
  locationStats: Array<{
    location: string
    count: number
  }>
}

export interface CreateReincarnationParams {
  soulId: string
  location: string
  newIdentity: string
  reason: string
  conditions: string
  remarks?: string
}

export interface UpdateReincarnationParams {
  location?: string
  newIdentity?: string
  reason?: string
  conditions?: string
  remarks?: string
  status?: 'pending' | 'processing' | 'completed'
}

const ReincarnationAPI = {
  list: '/api/reincarnations',
  stats: '/api/reincarnations/stats',
  create: '/api/reincarnations',
  get: (id: string) => `/api/reincarnations/${id}`,
  update: (id: string) => `/api/reincarnations/${id}`,
  delete: (id: string) => `/api/reincarnations/${id}`,
}

export const ReincarnationService = {
  async getReincarnations(params: ReincarnationParams) {
    return get<{ total: number; items: Reincarnation[] }>(ReincarnationAPI.list, params)
  },

  async getReincarnation(id: string) {
    return get<Reincarnation>(ReincarnationAPI.get(id))
  },

  async getStats() {
    return get<ReincarnationStats>(ReincarnationAPI.stats)
  },

  async createReincarnation(params: CreateReincarnationParams) {
    return post<Reincarnation>(ReincarnationAPI.create, params)
  },

  async updateReincarnation(id: string, params: UpdateReincarnationParams) {
    return put<Reincarnation>(ReincarnationAPI.update(id), params)
  },

  async deleteReincarnation(id: string) {
    return del(ReincarnationAPI.delete(id))
  },
}
